<script>
import outMain from "@/components/outMain.vue";
import defUpImg from "@/components/public/defUpImg.vue";
import cropperCom from "@/components/public/cropperCom_31.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Rights, deleteRights, addRights, editRights } from "@/http/api";
export default {
  components: { outMain, defUpImg, cropperCom },
  name: "",
  data() {
    return {
      showMask: false,
      tableData: [],
      queryParams: {
        rights_describe: "",
        rights_img: "",
        rights_name: "",
      },
      maskTitle: "",
      queryData: {
        page: 1,
        per_page: 10,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    async saveFn() {
      if (!this.queryParams.rights_img) {
        ElMessage("请选择图片");
        return false;
      }
      const { data } =
        this.maskTitle == "新增"
          ? await addRights(this.queryParams)
          : await editRights(this.queryParams);
      if (data.code == 1) {
        ElMessage({
          type: "success",
          message: this.maskTitle + "成功",
        });
        this.showMask = false;
        this.getData();
      }
    },
    editFn(row) {
      this.queryParams = {
        rights_describe: row.rights_describe,
        rights_name: row.rights_name,
        rights_img: row.rights_img,
        rights_id: row.rights_id,
      };
      this.showMask = true;
      this.maskTitle = "编辑";
    },
    addFn() {
      this.queryParams = {
        rights_describe: "",
        rights_img: "",
        rights_name: "",
      };
      this.showMask = true;
      this.maskTitle = "新增";
    },
    deleteFn(row) {
      ElMessageBox.confirm("确定删除选择的权益吗?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const { data } = await deleteRights({ rights_id: row.rights_id });
        if (data.code == 1) {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          this.getData();
        }
      });
    },
    getData() {
      Rights(this.queryData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
    getImg(data) {
      this.queryParams.rights_img = data;
    },
  },
  mounted() {},
  created() {
    this.getData();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>小程序端口权益配置{{ total }}）</div>
      </div>
      <div class="tableSerch hasBottom">
        <div class="serchLeft"></div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <btn value="新增" @ok="addFn"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column
          prop="staff_user_name"
          show-overflow-tooltip
          label="权益图片"
        >
          <template #default="{ row }">
            <img class="userImg" :src="row.rights_img" />
          </template>
        </el-table-column>
        <el-table-column
          prop="rights_name"
          show-overflow-tooltip
          label="权益名称"
        />
        <el-table-column
          prop="rights_describe"
          show-overflow-tooltip
          label="权益说明"
        />
        <el-table-column prop="Name" width="180" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div class="blueText ctrlText" @click="editFn(row)">编辑</div>
              <div class="redText ctrlText" @click="deleteFn(row)">删除</div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
    </div>
    <el-dialog
      v-model="showMask"
      :title="maskTitle + '权益'"
      width="500"
      :close-on-click-modal="false"
    >
      <div v-if="showMask">
        <el-form ref="ruleForm" :model="queryParams">
          <el-form-item label-width="120" label="权益名称" prop="rights_name">
            <el-input
              v-model="queryParams.rights_name"
              placeholder="权益名称"
            />
          </el-form-item>
          <el-form-item
            label-width="120"
            label="权益说明"
            prop="rights_describe"
          >
            <el-input
              v-model="queryParams.rights_describe"
              placeholder="权益说明"
            />
          </el-form-item>

          <el-form-item label-width="120" label="权益图片">
            <cropperCom
              :propImg="queryParams.rights_img"
              @successCropper="getImg"
            ></cropperCom>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showMask = false">取消</el-button>
          <el-button type="primary" @click="saveFn"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </outMain>
</template>
<style scoped lang="less"></style>
